package pub.tbc.toolkit.http2.template;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.fluent.Request;
import pub.tbc.toolkit.Objs;
import pub.tbc.toolkit.http2.ResponseWrapper;
import pub.tbc.toolkit.io.Files;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tbc on 2016/9/22 14:15:45.
 */
@Slf4j
public class SimpleHttpTemplate {

    ResponseWrapper get(String url, String... args) {
        for (String s : args) {
            url = url + "/" + s;
        }
        HttpResponse response;
        try {
            response = Request.Get(url)
                    .version(HttpVersion.HTTP_1_1)
                    .execute()
                    .returnResponse();
        } catch (IOException e) {
            log.error("请求外部系统失败，url：{}, message: {}", url, e.getMessage());
            throw new RuntimeException("请求外部系统失败", e);
        }
        assert Objs.nonEmpty(response);
        int httpCode = response.getStatusLine().getStatusCode();
        String contentType = response.getEntity().getContentType().getValue().split(";")[0];
        String content ;
        InputStream inputStream;
        try {
            Files.read(response.getEntity().getContent());
        } catch (IOException e) {
            log.error("请求外部系统失败，url：{}, message: {}", url, e.getMessage());
            throw new RuntimeException("请求外部系统失败", e);
        }

        HashMap<String, String> map = null;
        return ResponseWrapper.builder().httpCode(httpCode).result("").build();
    }


}
